//字符串有三种编辑操作:插入一个英文字符、删除一个英文字符或者替换一个英文字符。 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。 
//
// 
//
// 示例 1: 
//
// 
//输入: 
//first = "pale"
//second = "ple"
//输出: True 
//
// 
//
// 示例 2: 
//
// 
//输入: 
//first = "pales"
//second = "pal"
//输出: False
// 
//
// Related Topics 双指针 字符串 👍 230 👎 0

package leetcode.editor.cn;
//leetcode submit region begin(Prohibit modification and deletion)
class SolutionMian01_05 {
    public boolean oneEditAway(String first, String second) {
        String longer = first.length() > second.length() ? first : second;
        String slower = first.length() > second.length() ? second : first;
        if(longer.length() - slower.length() > 1){
            return false;
        }
        if (longer.length() == 1 && slower.length() == 0) {
            return true;
        }
        int i = 0, j = 0;
        if (longer.length() == slower.length()) {
            int num = 0;
            while (i < longer.length()) {
                if (longer.charAt(i) != slower.charAt(j)) {
                    num++;
                }
                i++;
                j++;
            }
            return num <= 1;
        }
        boolean flag = false;
        while (i < longer.length()) {
            if(j < slower.length() && longer.charAt(i) != slower.charAt(j)){
                if (flag) {
                    return false;
                }
                i++;
                flag = true;
                continue;
            }
            i++;
            j++;
        }
        return true;
    }

    public static void main(String[] args) {
        boolean b = new SolutionMian01_05().oneEditAway("a", "ab");
        System.out.println(b);
    }
}
//leetcode submit region end(Prohibit modification and deletion)
